package com.lkm.app_ui.m.Imple.hsj522;

import android.os.Handler;
import android.os.Looper;

import com.lkm.app_ui.AppConst;
import com.lkm.app_ui.log.SystemLogger;
import com.lkm.app_ui.m.nfc.NFC_Module;
import com.lkm.comlib.task2.DefaultTaskExecutor;
import com.lkm.frame.entity.ResponEntityAbs;
import com.lkm.frame.task.ProgressDataImpl;
import com.lkm.frame.task2.BackCallAdapter;
import com.lkm.frame.task2.Task;
import com.lkm.frame.task2.TaskAdapter;
import com.lkm.javatool.NumberHelp;
import com.lkm.javatool.StringHelp;
import com.lkm.javatool.lang.RunnablePR;



class WaitVipInfoForBlockTask extends TaskAdapter<Void,Void , String> {

    private NFC_Module hsj800tModule;

    private Handler handler;
    private int sectorIndex;
    private int blockIndex;
    private boolean isAddCardNo ;

    public WaitVipInfoForBlockTask(String id, NFC_Module hsj800tModule, boolean isAddCardNo, int sectorIndex, int blockIndex) {
        super(new DefaultTaskExecutor(AppConst.getTaskExecutor(), true), id, AppConst.getTaskManager2());
        handler = new Handler(Looper.getMainLooper());
        this.hsj800tModule = hsj800tModule;
        this.sectorIndex = sectorIndex;
        this.blockIndex = blockIndex;
        this.isAddCardNo = isAddCardNo;
    }

    @Override
    public void doExecuting(Void param) {
        super.doExecuting(param);
        xx();
    }

    private void xx() {

        hsj800tModule.setOnCardInRun(new RunnablePR<String, Void>() {
            @Override
            public Void run(String s) {
                hsj800tModule.setOnCardInRun(null);
                

                Task stringTask = hsj800tModule.tryReadBolck(sectorIndex, blockIndex);
                if (stringTask != null) {

                    stringTask.addBackCall(new BackCallAdapter<Object, ResponEntityAbs<byte[]>>() {
                        @Override
                        protected void onEnd(Task<?, Object, ResponEntityAbs<byte[]>> task, ResponEntityAbs<byte[]> re, boolean isCancel) {
                            super.onEnd(task, re, isCancel);
                            if (!getIsContinue()) return;
                            byte[] d=re == null ? null : re.getData();
                            String o = d == null ? null : NumberHelp.bytesTohexDesc(d);
                            if (!StringHelp.isEmpty(o)) {
                                if(isAddCardNo){
                                    o = s+","+o;
                                }

                                complete(o);
                            } else {
                                dexx();
                            }
                        }
                    });
                    if (stringTask.execTask() == serialNum_not) {
                        dexx();
                    }
                } else {
                    dexx();
                }


                return null;
            }
        });


    }


    private void dexx() {
        handler.removeCallbacksAndMessages(null);
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                if (getIsContinue())
                    xx();
            }
        }, 0);

    }
}
